<?php

namespace App\Modules\LuckDraw\Award;

use App\Http\Model\GiftPearl;
use App\Http\Model\GiftPearlBag;

class GiftPearlAward extends BaseAward implements AwardInterface {
    public $type = 3;//类型 0未中奖 1:礼物 2:商品 3:金豆礼物/珍珠礼物

    public function __construct(
        int $luckId,
        int $userId,
        int $awardId,
        int $timeLimit,
        int $price
    ){
        $this->luckId = $luckId;
        $this->userId = $userId;
        $this->awardId = $awardId;
        $this->timeLimit = $timeLimit;
        $this->price = $price;
    }
    public function save(): bool {
        // TODO: Implement save() method.
        return (new GiftPearlBag())->inc($this->userId, 0, $this->awardId, $this->num);
    }
    public function detail(){
        $gift = (new GiftPearl())->getGift($this->awardId);
        $this->name = $gift['name'];
        $this->image = $gift['image'];
        $this->animateResource = $gift['animate_resource']??'';
//        $this->price = $gift['price'] ?? 0;
        $this->_getAperture();
    }

    private function _getAperture(){
        $radio = config('pay.pearl_change_ratio.diamond_to_pearl');
        $price = $this->price / $radio;
        if ($price >= 0 and $price < 100000) {
            $this->aperture = 1;
        } elseif ($price >= 100000 and $price < 300000) {
            $this->aperture = 2;
        } elseif ($price >= 300000) {
            $this->aperture = 3;
        } else {
            $this->aperture = 0;
        }
    }
}